package kibahed.uni.fileinteractions;

import java.io.File;

import javax.swing.JFileChooser;

public class FileOperations {

	public static File openFile(final String[] extension, String title,int openingFileType, int openDialogType, String defaultPath) {
		final JFileChooser chooser = new JFileChooser(title);
		chooser.setDialogType(openDialogType);
		chooser.setFileSelectionMode(openingFileType);
		chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {

			@Override
			public boolean accept(File f) {
				if (f.isDirectory()) {
					return true;
				}
				for (String s : extension) {
					if (f.getName().endsWith(s)) {
						return true;
					}
				}
				return false;
			}

			@Override
			public String getDescription() {
				String description = "";
				for (String s : extension) {
					description += "*" + s + "; ";
				}
				return description;
			}
		});
		final File file = new File(defaultPath);

		chooser.setCurrentDirectory(file);

		chooser.setVisible(true);
		final int result = chooser.showOpenDialog(null);

		if (result == JFileChooser.APPROVE_OPTION) {
			File inputVerzFile = chooser.getSelectedFile();
			chooser.setVisible(false);
			return inputVerzFile;
		}
		chooser.setVisible(false);
		return null;
	}
	
	public static File saveFile(String title,int saveDialogType,int fileType, String filePath) {
		final JFileChooser chooser = new JFileChooser(title);
		chooser.setDialogType(saveDialogType);
		chooser.setFileSelectionMode(fileType);

		final File file;

		if (filePath != null) {
			file = new File(filePath);
			chooser.setCurrentDirectory(file);
			chooser.setSelectedFile(file);
			
		}
		
		chooser.setVisible(true);
		final int result = chooser.showSaveDialog(null);

		if (result == JFileChooser.APPROVE_OPTION) {
			File inputVerzFile = chooser.getSelectedFile();
			chooser.setVisible(false);
			filePath=chooser.getCurrentDirectory().getPath() + "/name";
			return inputVerzFile;
		}

		chooser.setVisible(false);
		return null;
	}
}
